package com.nantai.jiafang.nantaijiafang.activity.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;

import com.nantai.jiafang.nantaijiafang.R;
import com.nantai.jiafang.nantaijiafang.bean.ZhiJjian.MLBodyItem;
import com.nantai.jiafang.nantaijiafang.databinding.FragmentZhijianDetailItemBinding;
import com.nantai.jiafang.nantaijiafang.zhijian.fragment.ZhijianFragment_detail_add;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/11/2.
 */

public class ZhiJian_detailAdapter extends RecyclerView.Adapter<ZhiJian_detailAdapter.ViewHolder> {
    private Context mcontext;
    private List<MLBodyItem> mlistt;
    private int layoutidd;

    private FragmentManager mFragmentManager;
    private ArrayList<Boolean> flag;
    private String bodyId;//表头Id
    static class ViewHolder extends RecyclerView.ViewHolder {
        FragmentZhijianDetailItemBinding binding;

        public ViewHolder(FragmentZhijianDetailItemBinding view) {
            super(view.getRoot());
           this.binding=view;
        }
        public FragmentZhijianDetailItemBinding getBinding(){
            return binding;
        }
    }

    public ZhiJian_detailAdapter(List<MLBodyItem> listt, int layoutid, FragmentManager mFragmentManager,String bodyId) {
        mlistt = listt;
        layoutidd=layoutid;
        this.mFragmentManager=mFragmentManager;
        this.bodyId=bodyId;
        flag=new ArrayList<>();
        for (int i = 0; i < listt.size(); i++) {
            flag.add(false);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mcontext == null) {
            mcontext=parent.getContext();
        }
        FragmentZhijianDetailItemBinding binding= DataBindingUtil.inflate(LayoutInflater.from(mcontext),layoutidd,parent,false);
        return new ViewHolder(binding);
    }
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
       // String list_class=mlistt.get(position);
        holder.getBinding().setItem2(mlistt.get(position));
        holder.getBinding().ckSelect.setOnCheckedChangeListener(null);//先设置一次CheckBox的选中监听器，传入参数null
        holder.getBinding().ckSelect.setChecked(flag.get(position));//用数组中的值设置CheckBox的选中状态

        //再设置一次CheckBox的选中监听器，当CheckBox的选中状态发生改变时，把改变后的状态储存在数组中
        holder.getBinding().ckSelect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                flag.set(position,b);
            }
        });
        holder.getBinding().cardZhijianItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().postSticky(bodyId);
                mFragmentManager.beginTransaction()
                        .addToBackStack(null)
                        .setCustomAnimations(R.anim.in_from_left,R.anim.in_from_right,R.anim.out_from_left,R.anim.out_from_right)
                        .replace(R.id.container2, ZhijianFragment_detail_add.newinit(getdata(position)),"ZhijianFragment_detail_add")
                        .commit();
            }
        });

    }

    @Override
    public int getItemCount() {
        return mlistt.size();
    }
    public ArrayList<Boolean> getFlag(){
        return flag;
    }
    public String getGuid(){
        if (flag==null)
            return "";
        StringBuffer buffer=new StringBuffer();
        for (int i = 0; i <flag.size() ; i++) {
            if (flag.get(i)) {
                buffer.append(mlistt.get(i).getGuid()+",");
            }
        }
        if (buffer.toString().length()>0) {
            buffer.deleteCharAt(buffer.length() - 1);
        }
        return buffer.toString();
    }
    public void removeitem(int position){
        mlistt.remove(position);
        flag.remove(position);
        notifyDataSetChanged();
    }
    public MLBodyItem getdata(int n){
        return mlistt.get(n);
    }
}